package com.anjubao.parking.municipal.user.service;

import com.anjubao.parking.municipal.user.dto.DateCustomerDto;
import com.anjubao.parking.municipal.user.mapper.ParkingCustomerMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单统计
 * @author zhuchunfeng
 */
@Service
public class ParkingCustomerService {

    @Resource
    private ParkingCustomerMapper parkingCustomerMapper;

    /**
     * 会员增量按天统计
     * @param startTime 统计开始时间
     * @param endTime 统计结束时间
     * @return 每天增量
     */
    public List<DateCustomerDto> day(LocalDate startTime, LocalDate endTime) {

        List<DateCustomerDto> newRes=new ArrayList<>();
        List<DateCustomerDto> res=parkingCustomerMapper.day(startTime,endTime);
        for(;;startTime=startTime.plus(1, ChronoUnit.DAYS)){
            Boolean flat=false;
            for(DateCustomerDto dateCustomerDto:res){
                if(dateCustomerDto.getDate().equals(startTime.toString())){
                    newRes.add(dateCustomerDto);
                    flat=true;
                    break;
                }
            }
            if(flat==false){
                newRes.add(new DateCustomerDto().setDate(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))).setAmount(String.valueOf(0)));
            }
            if(startTime.equals(endTime)){
                break;
            }
        }
        return newRes;
    }


    /**
     * 会员增量按月统计
     * @param startTime 统计开始时间
     * @param endTime 统计结束时间
     * @return 每月增量
     */
    public List<DateCustomerDto> month(LocalDate startTime, LocalDate endTime) {
        List<DateCustomerDto> newRes=new ArrayList<>();
        List<DateCustomerDto> res=parkingCustomerMapper.month(startTime,endTime);
        for(;; startTime=startTime.plus(1, ChronoUnit.MONTHS)){
            Boolean flat=false;
            for(DateCustomerDto dateCustomerDto:res){
                if(startTime.toString().indexOf(dateCustomerDto.getDate())!=-1){
                    newRes.add(dateCustomerDto);
                    flat=true;
                    break;
                }
            }
            if(flat==false){
                newRes.add(new DateCustomerDto().setDate(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM"))).setAmount(String.valueOf(0)));
            }
            if(startTime.format(DateTimeFormatter.ofPattern("yyyy-MM")).equals(endTime.format(DateTimeFormatter.ofPattern("yyyy-MM")))){
                break;
            }
        }
        return newRes;
    }

    /**
     * 会员增量按年统计
     * @param startTime 统计开始时间
     * @param endTime 统计结束时间
     * @return 每年增量
     */
    public List<DateCustomerDto> year(LocalDate startTime, LocalDate endTime) {
        List<DateCustomerDto> newRes=new ArrayList<>();
        List<DateCustomerDto> res=parkingCustomerMapper.year(startTime,endTime);
        for(; ;startTime= startTime.plus(1, ChronoUnit.YEARS)){
            Boolean flat=false;
            for(DateCustomerDto dateCustomerDto:res){
                if(startTime.toString().indexOf(dateCustomerDto.getDate())!=-1){
                    newRes.add(dateCustomerDto);
                    flat=true;
                    break;
                }
            }
            if(flat==false){
                newRes.add(new DateCustomerDto().setDate(startTime.format(DateTimeFormatter.ofPattern("yyyy"))).setAmount(String.valueOf(0)));
            }
            if(startTime.format(DateTimeFormatter.ofPattern("yyyy")).equals(endTime.format(DateTimeFormatter.ofPattern("yyyy")))){
                break;
            }
        }
        return newRes;
    }

    public String total() {
        return parkingCustomerMapper.total();
    }
}

